package com.sdjnecc.myapplication;

import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by Jack Stone on 2016/11/17.
 * Socket服务器，PC可以通过USB连接、ADB端口映射连接本服务器，不需要通过Wifi和网络
 */

public class TCPConnect implements Runnable {
    private static final String TAG = "TCPConnect";
    private final int SERVER_PORT = 10086;
    private ServerSocket mServerSocket;
    private Socket mClient;
    private Callback callback;

    public TCPConnect(Callback callback) {
        this.callback = callback;
    }


    @Override
    public void run() {
        try {
            String ip = InetAddress.getLocalHost().getHostAddress();
            mServerSocket = new ServerSocket(SERVER_PORT);
            callback.call("建立服务器：[" + ip + ":" + SERVER_PORT + "]");
        } catch (IOException e) {
            callback.call("建立服务器异常：" + e.getMessage());
        }
        while (true) {

            BufferedOutputStream out = null;
            BufferedReader in = null;
            try {
                mClient = mServerSocket.accept();
                callback.call("建立链接：" + mClient.getInetAddress().toString() + ":" + mClient.getPort());
                out = new BufferedOutputStream(mClient.getOutputStream());
                in = new BufferedReader(new InputStreamReader(mClient.getInputStream()));

                String request = receive(in);
                callback.call("client:" + request);
                send(out, request);

            } catch (IOException e) {
                Log.e(TAG, "run: ", e);
                callback.call(e.getMessage());
            } finally {
                close(out);
                close(in);
                close(mClient);
            }
        }
    }

    private void send(OutputStream out, String msg) throws IOException {
        msg += "\n";
        out.write(msg.getBytes("utf-8"));
    }

    private String receive(BufferedReader in) throws IOException {
        String r = in.readLine();
        callback.call("origin request:" + r);
        if (r.contains("\\&")) {
            callback.call("进行\\&替换！");
            r = r.replaceAll("\\\\&", "\n");
        }
        return r;
    }

    private void close(OutputStream out) {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void close(BufferedReader in) {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void close(Socket socket) {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                Log.e(TAG, "run: ", e);
            }
        }
    }

    public interface Callback {
        void call(String msg);
    }
}